package com.xiaohe66.web.domain.birth.service;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.common.value.Paging;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.birth.agg.Birthday;
import com.xiaohe66.web.domain.birth.repository.BirthdayRepository;
import com.xiaohe66.web.domain.birth.value.BirthdayId;
import com.xiaohe66.web.domain.im.agg.Message;
import com.xiaohe66.web.domain.im.repository.MessageRepository;
import com.xiaohe66.web.domain.im.value.MessageContext;
import com.xiaohe66.web.domain.im.value.MessageId;
import com.xiaohe66.web.domain.im.value.MessageTitle;
import com.xiaohe66.web.domain.sys.sec.service.SecurityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaohe
 * @since 2023-07-12 15:41
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BirthdayService {

    private final BirthdayRepository birthdayRepository;
    private final SecurityService securityService;

    private final MessageRepository sysMessageRepository;

    public void deleteById(BirthdayId id){

        AccountId currentAccountId = securityService.getCurrentAccountId();
        securityService.checkCurrentAccountPermission(currentAccountId);

        birthdayRepository.removeById(id);
    }

    public List<Birthday> list(AccountId createId, Paging paging) {

        securityService.checkCurrentAccountPermission(createId);

        return birthdayRepository.list(createId, paging);
    }

    public List<Birthday> listAfterDay(AccountId createId, int day) {

        securityService.checkCurrentAccountPermission(createId);

        return birthdayRepository.listAfterDay(createId, day);
    }

    public void pushBirthdayMessage() {

        // 1. 查出所有用户
        // TODO : 暂时固定只有2个人
        List<AccountId> accountIdList = List.of(new AccountId(2), new AccountId(3));

        // 2. 查出该用户需要提醒的部分
        for (AccountId accountId : accountIdList) {

            // 今天
            List<Birthday> today = birthdayRepository.listAfterDay(accountId, 0);

            // 明天
            List<Birthday> tomorrow = birthdayRepository.listAfterDay(accountId, 1);

            if (today.isEmpty() && tomorrow.isEmpty()) {
                log.debug("pushBirthdayMessage empty, accountId : {}", accountId);
                continue;
            }

            StringBuilder contextBuilder = new StringBuilder();
            for (Birthday birthday : today) {
                contextBuilder.append('，').append(birthday.getName().getValue()).append("(今天)");
            }

            for (Birthday birthday : tomorrow) {
                contextBuilder.append('，').append(birthday.getName().getValue()).append("(明天)");
            }

            // 3. 推送
            Message message = Message.builder()
                    .id(new MessageId(IdWorker.genId()))
                    .createId(AccountId.SYSTEM_ID)
                    .accountId(accountId)
                    // .title(new MessageTitle("有好友要过生日啦"))
                    .context(new MessageContext(contextBuilder.substring(1)))
                    .build();

            sysMessageRepository.save(message);
        }
    }
}
